<?php
/**
 * Created by PhpStorm.
 * User: 76683
 * Date: 2018/7/21
 * Time: 0:31
 */

namespace app\sd\admin;


use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\sd\model\WeAds as AdModel;
use think\Request;

class Ad extends Admin
{
    public function index(){
        $map = $this->getMap();
        $data_list = AdModel::adsList($map);
        $columns = [
            ['id','ID'],
            ['name','广告名称'],
            ['app_id','APP_ID'],
            ['page','跳转页面'],
            ['type','广告类型','callback',function($value){
                switch ($value){
                    case 1:
                        return "H5广告";
                    case 2:
                        return '小程序广告';
                    default:
                        '数据错误';
                }
            }],
            ['is_top','是否悬浮','callback',function($value){
                return $value?'悬浮广告':'普通广告';
            }],
            ['banner','广告图','picture'],
            ['start_time','开始时间','datetime'],
            ['end_time','结束时间','datetime'],
            ['create_time','创建时间','datetime'],
            ['update_time','更新时间','datetime'],
            ['right_button', '操作', 'btn']
        ];
        $fields = [
            ['hidden','id'],
            ['hidden','create_time','创建时间'],
            ['select','type','广告类型','选择广告类型,默认H5广告，小程序广告必填APP_ID',['1'=>'H5广告','2'=>'小程序广告'],'1'],
            ['text','name','广告名称','小程序名称即可'],
            ['image','banner','广告图片'],
            ['text','app_id','APP_ID','小程序广告必填APP_ID。H5广告可不填,请将广告域名加入小程序业务域名以便正常加载'],
            ['text','page','跳转页面','小程序广告可为空，H5广告必填'],
            ['select','is_top','是否悬浮','默认 普通广告',['0'=>'普通广告','1'=>'悬浮广告'],'0'],
            ['datetime','start_time','开始时间'],
            ['datetime','end_time','结束时间']
        ];
        return ZBuilder::make('table')
            ->addColumns($columns)
            ->autoAdd($fields,'we_ads')
            ->autoEdit($fields,'we_ads')
            ->setTableName('we_ads')
            ->addRightButtons(['delete'])
            ->setRowList($data_list)
            ->addTimeFilter('start_time','','开始时间')
            ->setColumnWidth('start_time,end_time,create_time,update_time',120)
            ->setColumnWidth('id,right_button',60)
            ->fetch();
    }


    public function add(){
        if($this->request->isGet()){
            return parent::add();
        }
        if($this->request->isPost()){
            $time = time();
            $data = $this->request->post();
            Request::instance()->post(['start_time'=>strtotime($data['start_time'])]);
            Request::instance()->post(['end_time'=>strtotime($data['end_time']),'create_time'=>$time,'update_time'=>$time]);
            return parent::add();
        }
    }

    public function edit($id = '')
    {
        if($this->request->isGet()){
            $id =input('id');
            return parent::edit($id);
        }
        if($this->request->isPost()){
            $id =input('id');
            $data = $this->request->post();
            $start_time = strtotime($data['start_time']);

            $create_time = $data['create_time'];
            if ($create_time >= time(strtotime(date('Y-m-d',$start_time)))){
                $this->error('开始时间小于创建时间，请重新选择');
            }
            Request::instance()->post(['start_time'=>$start_time]);
            Request::instance()->post(['end_time'=>strtotime($data['end_time'])]);
            Request::instance()->post(['create_time'=>$create_time]);
            return parent::edit($id);
        }
    }
}